//
//  BankInfoVC.swift
//  LeGou
//
//  Created by lxm on 2022/9/4.
//

import UIKit

class BankInfoVC: ZMViewController {

    //MARK:--------------------属性--------------------
    
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var name: UILabel!
    
    @IBOutlet weak var cardInfo: UILabel!
    
    var info : MineModels.WalletInfo!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "银行卡信息"
        self.name.text = info.ownerName
        self.cardInfo.text = info.bankName + " " + info.bankNo.zm.hideBankNum()
    }
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        
    }

    @IBAction func onUnBind(_ sender: Any) {
        unBind()
    }
    
    func unBind() {
        if !ZMAppShareData.share.safeUserInfo.isTradeOk {
            // 判断是否设置交易密码
            ZMAlertView.show(content: "还未设置交易密码", left: "关闭", right: "马上去设置") {
                
            } onRight: {
                let cls = type(of: self)
                MineService.shared.tradeSuccessToVC = cls
                let vc = TradeFroget()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            return
        }
        
        ZMAlertView.show(title: "解除绑定", content: "解绑后会影响余额提现，是否确认解绑？", left: "取消", right: "确认") {
            
        } onRight: {
            let vc = PsdTradeCodeVerify()
            vc.onComplete = { [weak self] pwd in
                guard let self = self else {return}
                self.unbindAccount(tradePwd: pwd)
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func unbindAccount(tradePwd : String) {
        ZMToast.showActivity()
        MineAPI.unbindBank(["tradePwd" : tradePwd]).rxRequest(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            ZMAppShareData.share.requestUserInfo { ok in
                ZMToast.showToast(title: "已解绑")
                ZMNotification.shard.subject.onNext(.bindThirdSuccess)
                _ = ZMJumpHelper.jumpToClass(VC: self, toClass: ThirdAccountVC.classForCoder(), notContainAutoJump: true)
            }
        }).disposed(by: disposeBag)
    }
    //MARK:--------------------delegate--------------------

}
